<template>

  <div class="site-content site-content--home" v-if="showOrHiVisible === 0">
    您申请开通广告位,请耐心等待审核结果
  </div>
  <div class="site-content site-content--home" v-else-if="showOrHiVisible === 1">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
      <el-form-item label="顶部广告位链接" prop="url">
        <el-input v-model="dataForm.url" placeholder="顶部广告位链接"></el-input>
      </el-form-item>
      <el-form-item label="顶部图" prop="imgUrl">
        <el-input v-model="dataForm.imgUrl" placeholder="顶部图"></el-input>
      </el-form-item>
      <div v-show="wenzivisible">
        <el-form-item label="文字颜色" prop="fontColor">
          <el-color-picker v-model="dataForm.fontColor" ></el-color-picker>
        </el-form-item>
        <el-form-item label="边框颜色" prop="boldColor">
          <el-color-picker v-model="dataForm.boldColor" ></el-color-picker>
        </el-form-item>
        <el-form-item label="效果" >
          <el-checkbox v-model="dataForm.fontExpShan">闪烁</el-checkbox>        <el-checkbox v-model="dataForm.fontExpBian">边框</el-checkbox>
        </el-form-item>
        <el-form-item label="字体大小" prop="fontSize">
          <el-input v-model="dataForm.fontSize" placeholder="字体大小"></el-input>
        </el-form-item>
      </div>
      <el-form-item label="顶部广告类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择"  @change="wenzivisibleF(dataForm.type)">
          <el-option
            v-for="item in types"
            :key="item.type"
            :label="item.label"
            :value="item.type">
          </el-option>
        </el-select>
      </el-form-item>

      <div  v-if="showOrHiVisibleTwo === 0">
      <el-form-item label="浮动一链接" prop="url1">
        <el-input v-model="dataForm.url1" placeholder="浮动一链接"></el-input>
      </el-form-item>
      <el-form-item label="浮动一图（代码）" prop="imgUrl1">
        <el-input v-model="dataForm.imgUrl1" placeholder="浮动一图（代码）"></el-input>
      </el-form-item>
      </div>
      <div  v-if="showOrHiVisibleThress === 0">
      <el-form-item label="浮动二链接" prop="url2">
        <el-input v-model="dataForm.url2" placeholder="浮动二链接"></el-input>
      </el-form-item>
      <el-form-item label="浮动二图（代码）" prop="imgUrl2">
        <el-input v-model="dataForm.imgUrl2" placeholder="浮动二图（代码）"></el-input>
      </el-form-item>
      </div>
      <div  v-if="showOrHiVisibleFour === 0">
      <el-form-item label="浮动三链接" prop="url3">
        <el-input v-model="dataForm.url3" placeholder="浮动三链接"></el-input>
      </el-form-item>
      <el-form-item label="浮动三图（代码）" prop="imgUrl3">
        <el-input v-model="dataForm.imgUrl3" placeholder="浮动三图（代码）"></el-input>
      </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
  <div class="site-content site-content--home" v-else="">
    <el-button type="primary" @click="deleteHandle(userId)">申请开通广告位</el-button>
  </div>
</template>
<script>
  import API from '@/api'
  import store from '@/store'         // api: https://github.com/vuejs/vuex
  export default {
    data () {
      return {
        userId: 0,
        wenzivisible: false,
        showOrHiVisible: -1,
        showOrHiVisibleTwo: -1,
        showOrHiVisibleThress: -1,
        showOrHiVisibleFour: -1,
        advLevel: 1,
        types: [{
          type: 1,
          label: '代码'
        }, {
          type: 2,
          label: '图片'
        }, {
          type: 3,
          label: '文字'
        }, {
          type: 4,
          label: '退弹'
        }],
        dataForm: {
          id: 0,
          title: '',
          url: '',
          imgUrl: '',
          url1: '',
          fontColor: '#00ff00',
          boldColor: '#ffffff',
          fontExpShan: false,
          fontExpBian: false,
          fontSize: '18',
          imgUrl1: '',
          url2: '',
          imgUrl2: '',
          url3: '',
          imgUrl3: '',
          status: 1,
          createUserId: '',
          createTime: '',
          type: '',
          loca: '',
          orderNum: 1
        },
        dataRule: {
          imgUrl: [
            { required: true, message: '顶部图不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '顶部广告位链接不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '广告类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    activated () {
      this.getapplyInfo()
    },
    methods: {
      // 获取数据列表
      getapplyInfo () {
        this.userId = store.state.user.id
        API.basememberapply.infoByUserId(this.userId).then(({data}) => {
          if (data && data.code === 0) {
            if (data.basememberapply) {
              this.showOrHiVisible = data.basememberapply.status
            }
          }
        })
        API.basemember.infoByUserId(this.userId).then(({data}) => {
          if (data && data.code === 0) {
            if (data.basemember) {
              this.advLevel = data.basemember.advLevel
              if (this.advLevel === 2) {
                this.showOrHiVisibleTwo = 0
                this.showOrHiVisibleThress = -1
                this.showOrHiVisibleFour = -1
              } else if (this.advLevel === 3) {
                this.showOrHiVisibleTwo = 0
                this.showOrHiVisibleThress = 0
                this.showOrHiVisibleFour = -1
              } else if (this.advLevel === 4) {
                this.showOrHiVisibleTwo = 0
                this.showOrHiVisibleThress = 0
                this.showOrHiVisibleFour = 0
              } else {
                this.showOrHiVisibleTwo = -1
                this.showOrHiVisibleThress = -1
                this.showOrHiVisibleFour = -1
              }
            }
          }
        })
        this.$nextTick(() => {
          if (this.userId) {
            API.baseadv.infoByUserId(this.userId).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.url = data.baseadv.url
                this.dataForm.imgUrl = data.baseadv.imgUrl
                this.dataForm.fontColor = data.baseadv.fontColor
                this.dataForm.boldColor = data.baseadv.boldColor
                this.dataForm.fontExpShan = data.baseadv.fontExpShan
                this.dataForm.fontExpBian = data.baseadv.fontExpBian
                this.dataForm.fontSize = data.baseadv.fontSize
                this.dataForm.url1 = data.baseadv.url1
                this.dataForm.imgUrl1 = data.baseadv.imgUrl1
                this.dataForm.url2 = data.baseadv.url2
                this.dataForm.imgUrl2 = data.baseadv.imgUrl2
                this.dataForm.url3 = data.baseadv.url3
                this.dataForm.imgUrl3 = data.baseadv.imgUrl3
                this.dataForm.type = data.baseadv.type
                if (this.dataForm.type === 3) {
                  this.wenzivisible = true
                } else {
                  this.wenzivisible = false
                }
              }
            })
          }
        })
      },
      wenzivisibleF (value) {
        if (value === 3) {
          this.wenzivisible = true
        } else {
          this.wenzivisible = false
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'title': this.dataForm.title,
              'url': this.dataForm.url,
              'imgUrl': this.dataForm.imgUrl,
              'fontColor': this.dataForm.fontColor,
              'boldColor': this.dataForm.boldColor,
              'fontExpShan': this.dataForm.fontExpShan === true ? 1 : 0,
              'fontExpBian': this.dataForm.fontExpBian === true ? 1 : 0,
              'fontSize': this.dataForm.fontSize,
              'url1': this.dataForm.url1,
              'imgUrl1': this.dataForm.imgUrl1,
              'url2': this.dataForm.url2,
              'imgUrl2': this.dataForm.imgUrl2,
              'url3': this.dataForm.url3,
              'imgUrl3': this.dataForm.imgUrl3,
              'status': 1,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime,
              'type': this.dataForm.type,
              'loca': this.dataForm.loca,
              'orderNum': this.dataForm.orderNum
            }
            var tick = API.baseadv.proxySave(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 删除
      deleteHandle (id) {
        var params = {
          userId: store.state.user.id
        }
        this.$confirm(`确定申请广告位操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          API.basememberapply.add(params).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getapplyInfo()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
